package com.jielihaofeng.nettyadvance.chat.server.handler;

import com.jielihaofeng.nettyadvance.chat.message.GroupChatRequestMessage;
import com.jielihaofeng.nettyadvance.chat.message.GroupChatResponseMessage;
import com.jielihaofeng.nettyadvance.chat.server.session.GroupSessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.List;

/**
 * @author Johnnie Wind
 * @description 群组聊天消息处理器
 * @date 2021/11/18 15:02
 */
@ChannelHandler.Sharable
public class GroupChatRequestMessageHandler extends SimpleChannelInboundHandler<GroupChatRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupChatRequestMessage message) throws Exception {
        String from = message.getFrom();
        String groupName = message.getGroupName();
        String content = message.getContent();

        List<Channel> channels = GroupSessionFactory.getGroupSession().getMembersChannel(groupName);
        channels.stream().forEach(channel -> channel.writeAndFlush(new GroupChatResponseMessage(from, content)));
    }
}
